<!DOCTYPE html>
<html>
<head>
    <include file="Public:header" />
    <!-- jqgrid-->
    <link href="__ACSS__/plugins/jqgrid/ui.jqgrid.css?0820" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
    	<input type="hidden" value="<{$vo.id}>" id="id"/>
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox ">
					<div class="ibox-content">
						<div class="ibox-search-box">
							<h3>选择店铺</h3>
						</div>
						<p>
							<span>活动参加店铺：</span>
							<select class="form-control" name="shop_id" datatype="*" id="shop_id">
								<option value="0" >请选择超市</option>
								<foreach name="shop_list" item="shop_list">
									<option value="<{$shop_list.id}>" ><{$shop_list.name}></option>
								</foreach>
							</select>
						</p>
					</div>
				</div>
			</div>
		</div>
        <div class="row">
            <div class="col-sm-6">
                <div class="ibox ">
                    <div class="ibox-content">
                    	<div class="ibox-search-box">
                    		<h3><font style="color:#ff5500">“<{$vo.title}>”</font> 选择学校</h3>
                    	</div>
						<div class="ibox-selected-search-box" style="border-bottom:0; margin:0; padding:0">
							<div class="row">
								<div class="infont col-md-2">
									<button type="button" id="del" class="btn btn-white btn-sm btn-outline"><i class="fa fa-trash-o"></i>&nbsp;删除</button>
								</div>
								<div class="infont col-md-4">
									<input type="text" name="name" class="form-control" placeholder="产品名">
								</div>
								<div class="infont col-md-2">
									<button class="btn btn-success btn-block btn-ibox-selected-search" type="button">搜索</button>
								</div>
							</div>
						</div>
                        <div class="jqGrid_wrapper">
                            <table id="table_list"></table>
                            <div id="pager_list"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="ibox ">
                    <div class="ibox-content">
                    	<div class="ibox-search-box">
                    		<h3>所有产品列表</h3>
                    	</div>
                    	<div class="ibox-search-box" style="border-bottom:0; margin:0; padding:0">
                    		<div class="row">
	                    		<div class="infont col-md-4">
		                    		<input type="text" name="name" class="form-control" placeholder="产品名">
								</div>
								<div class="infont col-md-4">
									<select name="type_id" class="form-control" placeholder="类型">
										<foreach name="types" item="types">
											<option value="<{$types.id}>"><{$types.name}></option>
										</foreach>
									</select>
								</div>
	                    		<div class="infont col-md-4">
	                    			<button class="btn btn-success btn-block btn-ibox-search" type="button">搜索</button>
	                    		</div>
                    		</div>
                    	</div>
                        <div class="jqGrid_wrapper">
                            <table id="table_list2"></table>
                            <div id="pager_list2"></div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
<!-- 全局js -->
<include file="Public:foot" />
<!-- jqGrid -->
<script src="__AJSPLUGINS__/jqgrid/i18n/grid.locale-cn.js?0820"></script>
<script src="__AJSPLUGINS__/jqgrid/jquery.jqGrid.min.js?0820"></script>
<!-- Page-Level Scripts -->
<script>
$(document).ready(function () {
	//初始化
    $.jgrid.defaults.styleUI = 'Bootstrap';

  //搜索
    $(".btn-ibox-search").click(function() {
    	result = getSearchParamByInput();
    	$("#table_list2").jqGrid('setGridParam', { 
    		page: 1,
    		url : '__URL__/getProductAllData' + result
    	}).trigger("reloadGrid");
    })
	//已选择商品搜索
	$(".btn-ibox-selected-search").click(function() {
		result = getSelectedSearchParamByInput();
		$("#table_list").jqGrid('setGridParam', {
			page: 1,
			url : '__URL__/getMeetingplaceProductData?id=<{$vo.id}>' + result
		}).trigger("reloadGrid");
	})
    //监控回车
    $(".ibox-search-box").find(":input").keydown(function(event) {
		if(event.keyCode == 13){
			$(".btn-ibox-search").trigger("click");
		}
	})
	//已选择监控回车
	$(".ibox-selected-search-box").find(":input").keydown(function(event) {
		if(event.keyCode == 13){
			$(".btn-ibox-selected-search").trigger("click");
		}
	})
	$("#shop_id").change(function(){
		var shop_id = $(this).val();
		if(!shop_id){
			shop_id=0;
		}
		//已选择产品
		if($("#table_list").find(".jqgfirstrow").length>0){
			$("#table_list").jqGrid('setGridParam', {
				page: 1,
				url : '__URL__/getMeetingplaceProductData?id=<{$vo.id}>&shop_id=' + shop_id
			}).trigger("reloadGrid");
		}else{
			$("#table_list").jqGrid({
				url : '__URL__/getMeetingplaceProductData?id=<{$vo.id}>&shop_id=' + shop_id,
				datatype: "json",
				height: 380,
				autowidth: true,
				shrinkToFit: true,
				multiselect:true,
				rowNum: 10,
				rowList: [10, 15, 30, 50],
				colNames: ['ID', '产品名称'],
				colModel: [ {name : 'id',index : 'id'},
					{name : 'name',index : 'name'},
				],
				pager: "#pager_list",
				viewrecords: true,
				hidegrid: false,
				sortname: "seq",
				sortorder: "desc"
			});
			// 设置按钮
			$("#table_list").jqGrid('navGrid', '#pager_list', {
				edit: false,
				add: false,
				del: false,
				search: false
			}, {
				height: 200,
				reloadAfterSubmit: true
			});
			// 添加适配响应
			$(window).bind('resize', function () {
				var width = $('.jqGrid_wrapper').width();
				$('#table_list').setGridWidth(width);
			});
		}
		//所有店铺商品
		if($("#table_list2").find(".jqgfirstrow").length>0){
			$("#table_list2").jqGrid('setGridParam', {
				page: 1,
				url : '__URL__/getProductAllData?shop_id=' + shop_id
			}).trigger("reloadGrid");
		}else{
			//产品列表
			$("#table_list2").jqGrid({
				url : '__URL__/getProductAllData?shop_id=' + shop_id,
				datatype: "json",
				height: 380,
				autowidth: true,
				shrinkToFit: true,
				rowNum: 10,
				rowList: [10, 15, 30, 50],
				colNames: ['产品名称', '类别', '操作'],
				colModel: [ {name : 'name',index : 'name'},
					{name : 'type_name',index : 'type_name'},
					{name : 'id',index : 'id',formatter : function(cellvalue, options, rowObject){
						content = '<button class="btn btn-white btn-xs btn-outline" onclick="addToList('+ cellvalue +')"><i class="fa fa-a"></i>  添加至列表</button>';
						return content;
					}}
				],
				pager: "#pager_list2",
				viewrecords: true,
				hidegrid: false,
				sortname: "seq"
			});
			// 设置按钮
			$("#table_list2").jqGrid('navGrid', '#pager_list2', {
				edit: false,
				add: false,
				del: false,
				search: false
			}, {
				height: 200,
				reloadAfterSubmit: true
			});
			// 添加适配响应
			$(window).bind('resize', function () {
				var width = $('.jqGrid_wrapper').width();
				$('#table_list2').setGridWidth(width);
			});
		}



	});
    //获取搜索条件（仅限封装简单条件）
    function getSearchParamByInput() {
    	var result = "";
    	$(".ibox-search-box").find(":input").each(function() {
    		if($(this).val() != "") {
    			if(result != "") {
    				result += "&";
    			}else{
    				result += "?";
    			}
    			result += $(this).attr("name") + "=" + $(this).val();
    		}
    	})
		var shop_id = $("#shop_id").val();
		if(!shop_id){
			shop_id=0;
		}
		result += "&shop_id=" + shop_id;
    	return result;
    }

	//获取已选择产品搜索条件（仅限封装简单条件）
	function getSelectedSearchParamByInput() {
		var result = "";
		$(".ibox-selected-search-box").find(":input").each(function() {
			if($(this).val() != "") {
				if(result != "") {
					result += "&";
				}else{
					result += "&";
				}
				result += $(this).attr("name") + "=" + $(this).val();
			}
		})
		var shop_id = $("#shop_id").val();
		if(!shop_id){
			shop_id=0;
		}
		result += "&shop_id=" + shop_id;
		return result;
	}

  	//删除
	$("#del").click(function() {
		var shop_id = $("#shop_id").val();
		if(!shop_id){
			shop_id=0;
		}
		var id = $("#table_list").jqGrid('getGridParam','selarrrow');
    	if(id != null) {
    		layer.confirm('确定执行？', function(index){
        		$.ajax({
        			url:"__URL__/del",
        			type:"post",
        			data:{'id':id,'shop_id':shop_id},
        			success:function(json){
        				var data = eval("("+json+")");
        				if(data.status == 1){
        					layer.msg(data.info);
        					$('#table_list').trigger("reloadGrid");
        				}else{
        					layer.msg(data.info);
        				}
        			}
        		})
        	    layer.close(index);
        	});
    	}else {
    		layer.msg("您没有选取数据！");
    	}
	})
});
//添加至列表
function addToList(id){
	var shop_id = $("#shop_id").val();
	if(!shop_id){
		shop_id=0;
	}
	$.ajax({
		url:"__URL__/addToList",
		type:"post",
		data:{'product_id':id,'meetingplace_id':$("#id").val(),'shop_id':shop_id},
		success:function(json){
			var data = eval("("+json+")");
			if(data.status == 1){
				layer.msg(data.info, {time: 1000});
			}else{
				layer.msg(data.info, {time: 1000});
				$('#table_list').trigger("reloadGrid");
			}
			$('#table_list').trigger("reloadGrid");
		}
	})
}
</script>
</html>